package com.fulihui.wgj.config;

import com.fulihui.common.util.JedisUtil;
import com.fulihui.common.util.PlatformUtil;
import com.fulihui.wgj.dal.mybatis.domain.WxPlatform;
import com.fulihui.wgj.dal.mybatis.repository.WxPlatformRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.List;

/**
 * jedis工具类初始化--主要用于将 公众号信息 同步到redis 减少数据库访问
 *
 * @author wang_wx
 * @date 2018-06-22
 */
@Configuration
public class PlatformUtilConfig {

    @Resource
    WxPlatformRepository wxPlatformRepository;

    @Autowired
    JedisUtil jedisUtil;

    @Bean
    public PlatformUtil platformUtil() {
        PlatformUtil platformUtil = new PlatformUtil();
        platformUtil.setJedisUtil(jedisUtil);
        loadAccessToken(platformUtil);
        return platformUtil;
    }

    private void loadAccessToken(PlatformUtil platformUtil) {
        List<WxPlatform> platformList = wxPlatformRepository.selectWxPlatformsStatusNormal();
        platformList.forEach(p -> platformUtil.set(p.getAppId(), p.getAuthorizerAccessToken()));
    }
}
